package backend

import (
	"fmt"
	"log"

	"github.com/Qsnh/goa/models"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
)

type WeiXinController struct {
	Base
}

// @router /backend/weixin [get]
func (this *WeiXinController) Index() {
	logs.Info("WeiXinController")
	// 获取渠道数据
	db := orm.NewOrm()
	channels := []models.WXChannel{}
	sql := `
	select c.*,t.n as subnum from 
	wx_channel c left outer join
	(select 
	event_key,
	count(distinct from_user_name) n
	from wx_event 
	where 
	event = 'subscribe'
	group by event_key) t 
	on t.event_key = c.scene_key
	`
	logs.Warn(sql)
	_, err := db.Raw(sql).QueryRows(&channels)
	fmt.Println(channels)
	log.Println(err)
	/*if err != nil {
		logs.Info(err)
		this.StopRun()
	}*/
	log.Println(channels)
	this.Data["json"] = map[string]interface{}{"channels": channels}
	this.ServeJSON()
	this.StopRun()
}
